/**
 * @author Administrator
 */


(function($){
	$.fn.banner = function(options){
		var settings = {
			intervalTime : 6000,
			switchTime : 1000
		};
		$.extend(settings,options);
		var mbanner = {};
		
		mbanner.selector = $(this).selector;
		if($(this).length>1){
			$.error('more than one selected object');
			return false;
		}
		mbanner.self = this;
		mbanner.height = 660;
		mbanner.lindex = 0;
		mbanner.indexbanner = 0;
		mbanner.indexnav = 0;
		mbanner.con = mbanner.self.find('.move-con');
		mbanner.ud = mbanner.con; 
		mbanner.conli = mbanner.con.find('.move-li');
		mbanner.size = mbanner.conli.size();
		mbanner.nav = mbanner.self.find('.move-nav');
		if(mbanner.size<=1){
			$(mbanner.nav).hide();
			return false;
		}

		var moveInterval;

		mbanner.initmoveon = function(){
			var i = 0;
			var navstr = "";
			while(i<mbanner.size){
				navstr = navstr+"<span></span>";
				i++;	
			}
			$(mbanner.nav).html(navstr);
			mbanner.navli = $(mbanner.nav).find('span');
			mbanner.firstconli = mbanner.conli[0];
			$(mbanner.firstconli).clone(true).appendTo($(mbanner.ud));
			mbanner.conli = mbanner.con.find('.move-li');
			mbanner.size = mbanner.conli.size();	
		};			

		mbanner.nextmoveon = function(){
			mbanner.lindex = mbanner.indexbanner;
			mbanner.indexbanner++;
			mbanner.switchmoveon();				
		};
		mbanner.switchmoveon = function(){	
			var h = mbanner.indexbanner* mbanner.height;
			mbanner.conli.removeClass('moveon');	
			mbanner.conli.eq(mbanner.indexbanner).addClass('moveon');
			mbanner.ud.animate({top:-h},settings.switchTime,function(){
				if(mbanner.indexbanner==mbanner.size-1){
					mbanner.lindex = mbanner.size-1;
					mbanner.indexbanner = 0;
					mbanner.ud.css({top:0});
				}																			 
			});
			if(mbanner.indexbanner==mbanner.size-1){
				mbanner.indexnav = 0;	
			}
			else{
				mbanner.indexnav = mbanner.indexbanner;	
			}
			mbanner.navli.removeClass('navon');	
			mbanner.navli.eq(mbanner.indexnav).addClass('navon');						
		};
		mbanner.run = function(){
			mbanner.initmoveon();
			mbanner.switchmoveon();
			moveInterval = setInterval(mbanner.nextmoveon,settings.intervalTime+settings.switchTime);
			mbanner.self.mouseover(function(){
				clearInterval(moveInterval)	;								
			});

			mbanner.self.mouseleave(function(){
				moveInterval = setInterval(mbanner.nextmoveon,settings.intervalTime+settings.switchTime);					
			});

			mbanner.navli.each(function(){
				var element = this;
				$(element).click(function(){
					if(!mbanner.ud.is(":animated")){						  
						mbanner.lindex = mbanner.indexbanner;	
						mbanner.indexbanner = $(this).index();	
						mbanner.switchmoveon();
					}
				});		
			});	
		};		
		mbanner.run();
	};
})(jQuery);
